﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using NoteSystem.utils;
namespace NoteSystem
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["accountCookie"] != null && Request.Cookies["pwdCookie"] != null)
            {
                string account = HttpUtility.UrlDecode(Request.Cookies["accountCookie"].Value);
                string pwd = HttpUtility.UrlDecode(Request.Cookies["pwdCookie"].Value);
                DataSet ds = DbHelper.Query($"select u.*,c.city_name from users as u  inner join citys as c  on u.city_id=c.id where u.account='{account}' and u.pwd='{pwd}'");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    Session["User"] = ds;
                    Response.Redirect("Home.aspx");
                }
            }
        }


        protected void btnLogin_Click(object sender, EventArgs e)
        {
            #region 登录
            //1.获取用户输入的账号密码信息 验证码 
            string account = txtAccount.Text.Trim();
            string pwd = txtPwd.Text.Trim();
            string code = txtCode.Text.Trim();//用户输入的验证码
            //2.验证验证码
            if (Session["Code"].ToString() != code)
            {
                Response.Write("<script>alert('验证码错误');</script>");
                return;
            }
            //3.校验账号密码
            DataSet ds = DbHelper.Query($"select u.*,c.city_name from users as u  inner join citys as c  on u.city_id=c.id where u.account='{account}' and u.pwd='{pwd}'");
            if (ds.Tables[0].Rows.Count <= 0)
            {
                Response.Write("<script>alert('账号密码错误');</script>");
                return;
            }
            //4.审批状态验证
            int type = Convert.ToInt32(ds.Tables[0].Rows[0]["type"]);
            if (type == 0)
            {
                Response.Write("<script>alert('账号未审批请耐心等待');</script>");
                return;
            }
            //5.存Session
            Session["User"] = ds;
            //6.记住我
            if (cbxRemark.Checked == true)
            {
                HttpCookie accountCookie = new HttpCookie("accountCookie");
                accountCookie.Value = HttpUtility.UrlEncode(account);
                accountCookie.Expires = DateTime.Now.AddMonths(1);

                HttpCookie pwdCookie = new HttpCookie("pwdCookie");
                pwdCookie.Value = HttpUtility.UrlEncode(pwd);
                pwdCookie.Expires = DateTime.Now.AddMonths(1);
                Response.Cookies.Add(accountCookie);
                Response.Cookies.Add(pwdCookie);
            }
            //7.更新登录次数和最后登录时间
            DbHelper.ExecuteSql($"update users set login_count=login_count+1,last_login_time='{DateTime.Now}' where id={ds.Tables[0].Rows[0]["id"]}");
            //8.跳页
            Response.Redirect("Home.aspx");
            #endregion
        }
    }
}